﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using TimersXP.Messaging;
using System.Windows;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace TimersXP
{
    /// <summary>A base class for the ViewModel classes.</summary>
    public abstract class ViewModelBase
    {
        /// <summary>Class Boolean Variable that is a nullable value tells if the ViewModel implementing ViewModelBase is in design mode or not.</summary>
        private static bool? isInDesignMode;

        #region Constructors
        /// <summary>Initializes a new instance of the <see cref="ViewModelBase"/> class.</summary>
        /// <NOTE>// " : this(null)" calls the other constructor and passes null. Constructor overloading</NOTE>
        protected ViewModelBase() : this(null) { }

        /// <summary>Initializes a new instance of the <see cref="ViewModelBase"/> class.</summary>
        /// <param name="messenger">An instance of a <see cref="Messenger"/> used to broadcast messages to other objects.
        /// If null, this class will attempt to broadcast using the Messenger's default instance.</param>
        protected ViewModelBase(IMessenger messenger) { MessengerInstance = messenger; }
        #endregion Constructors

        #region Properties
        /// <summary>Occurs when [property changed].</summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Gets a value indicating whether [is in design mode static] is in-fact in design mode (running in Blend or Visual Studio)
        /// false to indicate that it is not in design mode but in run-time mode.
        /// </summary>
        /// <value>c>true</c> if [is in design mode static]; otherwise, <c>false</c>.</value>
        /// <NOTE>
        /// SuppressMessage is used by System.Diagnostics.CodeAnalysis. When you are applying code analysis to your project you will sometimes
        /// get warnings that you want to ignore. You do that by using SuprpressMessageAttribute. Now code analysis is built into the "higher" SKU's
        /// of Visual Studio but previously you had to use the tool named FxCop to prerform the analysis. Using code analysis on your code will not only
        /// increase the quality of your code but you might also learn a few things in the process.
        /// http://stackoverflow.com/questions/11910764/whats-the-use-of-supressmessage-attribute
        /// </NOTE>
        [SuppressMessage(
            "Microsoft.Security",
            "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands",
            Justification = "The security risk here is neglectible.")]
        public static bool IsInDesignModeStatic
        {
            get
            {
                if (!isInDesignMode.HasValue)
                {
#if SILVERLIGHT
                    isInDesignMode = DesignerProperties.IsInDesignTool;
#else
                    var prop = DesignerProperties.IsInDesignModeProperty;
                    isInDesignMode =
                        (bool)DependencyPropertyDescriptor.FromProperty(prop, typeof(FrameworkElement)).Metadata.DefaultValue;

                    // Just to be sure
                    if (!isInDesignMode.Value && Process.GetCurrentProcess().ProcessName.StartsWith("devenv", StringComparison.Ordinal))
                        isInDesignMode = true;
#endif
                }
                return isInDesignMode.Value;
            }
        }

        /// <summary>
        /// Gets a value indicating whether [is in design mode static] is in-fact in design mode (running in Blend or Visual Studio)
        /// false to indicate that it is not in design mode but in run-time mode.
        /// </summary>
        /// <value><c>true</c> if [is in design mode]; otherwise, <c>false</c>.</value>
        /// <NOTE>
        /// SuppressMessage is used by System.Diagnostics.CodeAnalysis. When you are applying code analysis to your project you will sometimes
        /// get warnings that you want to ignore. You do that by using SuprpressMessageAttribute. Now code analysis is built into the "higher" SKU's
        /// of Visual Studio but previously you had to use the tool named FxCop to prerform the analysis. Using code analysis on your code will not only
        /// increase the quality of your code but you might also learn a few things in the process.
        /// http://stackoverflow.com/questions/11910764/whats-the-use-of-supressmessage-attribute
        /// </NOTE>
        [SuppressMessage(
            "Microsoft.Performance",
            "CA1822:MarkMemberAsStatic",
            Justification = "Non static member needed for data binding")]
        public bool IsInDesignMode
        {
            get { return IsInDesignModeStatic; }
        }

        /// <summary>
        /// Gets or sets an instance of a <see cref="IMessenger"/> used to broadcast messages to other objects.
        /// if null, then class will attempt to broadcast using the Messenger's default instance.
        /// </summary>
        /// <value>The messenger instance.</value>
        protected IMessenger MessengerInstance
        {
            get;
            set;
        }
        #endregion Properties

        #region Functions
        /// <summary>
        /// Broadcasts a PropertyChangedMessage using either the instance of the Messenger that was passed to this class (if available)
        /// or the Messenger's default instance.
        /// </summary>
        /// <typeparam name="T">The type of the property that changed.</typeparam>
        /// <param name="oldValue">The value of the property before it changed.</param>
        /// <param name="newValue">The value of the property after it changed.</param>
        /// <param name="propertyName">Name of the property that changed.</param>
        protected virtual void Broadcast<T>(T oldValue, T newValue, string propertyName)
        {
            var message = new PropertyChangedMessage<T>(this, oldValue, newValue, propertyName);

            if (MessengerInstance != null)
            {
                MessengerInstance.Send(message);
            }
            else
                Messenger.
        }
        #endregion Functions
    }
}
